package d_routing_direct;

import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import java.io.IOException;
import org.junit.Test;
import utils.RabbitMQUtils;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 雷哥
 * @Date: 2020/10/06/23:41
 * @Description: 生产者
 */
public class Consumer1 {

  @Test
  public void receiveMessage() throws  Exception{
    Connection connection = RabbitMQUtils.getConnection();
    //创建通道
    Channel channel = connection.createChannel();
    //设置交换机
    channel.exchangeDeclare("logs", BuiltinExchangeType.DIRECT);
    //从通道里面得到一个临时队列
    String queue = channel.queueDeclare().getQueue();
    //把临时队列和交换机进行绑定
    channel.queueBind(queue,"logs","info");
    channel.queueBind(queue,"logs","warm");
    channel.queueBind(queue,"logs","debug");
    channel.queueBind(queue,"logs","error");
    //接收消息
    channel.basicConsume(queue,new DefaultConsumer(channel){
      @Override
      public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties,
          byte[] body) throws IOException {
        System.out.println("消费者【info】接收到消息:"+new String(body));
      }
    });
    System.out.println("消费者【info】启动成功");
    System.in.read();
  }

}
